package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.TransferGoodsConfirmDetailActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.TransGoodsBean;
import com.supply.dl.bean.request.TransGoodsRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 移库收货
 * Created by Leng on 2018/8/31.
 */
public class TakeDeliveryModel extends ISearchModle<TransGoodsBean> {

    @Override
    public void setSearchAdapter() {
        if (adapter != null)
            return;

        resource = new ArrayList<>();
        adapter = new NormalAdapter<TransGoodsBean>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, TransGoodsBean data) {
                holder.setText(R.id.txt_name, "移库单号" + data.orderNo);
                holder.setText(R.id.txt_status, data.noticeOrderStatusName);
                holder.setText(R.id.txt_type, "移库单类型:" + data.orderTypeName);
                holder.setText(R.id.txt_out, "出库仓库:" + data.tfOutWhName);
                holder.setText(R.id.txt_in, "入库仓库:" + data.tfInWhName);
                holder.setText(R.id.txt_time, StringUtils.isEmpty(String.valueOf(data.addTime)) ? ""
                        : DateUtils.getSimpleDateTimeByMill(String.valueOf(data.addTime), DateUtils.DATE_TIME_FORMAT));//建单时间
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_change_take_delivery;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<TransGoodsBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, TransGoodsBean data, int position) {
                Bundle b = new Bundle();
                b.putSerializable("TransGoodsBean",data);
                BaseActivity ac = (BaseActivity) context;
                ac.jumpActivity(TransferGoodsConfirmDetailActivity.class,b);
            }
        });
    }

    @Override
    public void setHintText() {
        hintText = "移库单号";
    }

    @Override
    public void setTitleShow() {
        titleShow = "移库收货";
    }

    @Override
    public void init(Context context) {
        this.context = context;
        setTitleShow();
        setSearchAdapter();
    }

    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {

        TransGoodsRequest transGoodsRequest = new TransGoodsRequest();
        transGoodsRequest.key = StringUtils.isCanUse(keyWord) ? keyWord : "";

        if (filterMap != null) {
            if(filterMap.containsKey(AppConfig.SearchFilterAction.MOVE_HOURSE_ORDER_TYPE))
                transGoodsRequest.orderType = filterMap.get(AppConfig.SearchFilterAction.MOVE_HOURSE_ORDER_TYPE);
            if(filterMap.containsKey(AppConfig.SearchFilterAction.MOVE_HOUSER_ORDER_STATUS))
                transGoodsRequest.orderStatus = filterMap.get(AppConfig.SearchFilterAction.MOVE_HOUSER_ORDER_STATUS);
            if (filterMap.containsKey(AppConfig.SearchFilterAction.START_TIME))
                transGoodsRequest.createTimeStart = filterMap.get(AppConfig.SearchFilterAction.START_TIME);
            if (filterMap.containsKey(AppConfig.SearchFilterAction.END_TIME))
                transGoodsRequest.createTimeEnd = filterMap.get(AppConfig.SearchFilterAction.END_TIME);
        } else {
            transGoodsRequest.orderType = "";
            transGoodsRequest.orderStatus = "";
            transGoodsRequest.createTimeStart = "";
            transGoodsRequest.createTimeEnd = "";
        }

        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.getTransGoodsList(transGoodsRequest, String.valueOf(pageNum), HttpManger.PAGE_SIZE)
                .compose(RxSchedulers.<BaseInfo<List<TransGoodsBean>>>compose(context))
                .subscribe(new BaseObserver<List<TransGoodsBean>>() {
                    @Override
                    public void onSuccess(BaseInfo<List<TransGoodsBean>> baseInfo) {
                        if(pageNum == 1){
                            resource.clear();
                        }
                        if(baseInfo.getData().size() < 10){
                            hasMoreData = false;
                        }
                        resource.addAll(baseInfo.getData());
                        adapter.notifyDataSetChanged();

                        if(resultListener != null)
                            resultListener.onResult();
                    }

                    @Override
                    public void onFailure(String msg) {
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                        if(resultListener != null)
                            resultListener.onResult();
                    }
                });
    }
}
